package com.tbit.main.controller;


import com.tbit.main.common.JsonResult;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AdoptMachineBound;
import com.tbit.main.pojo.OrderDateSta;
import com.tbit.main.pojo.RideSta;
import com.tbit.main.service.AdoptMachineBoundService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 领养管理
 *
 * @author Leon
 * 2020年9月1日 下午5:36:15
 */
@RestController
@RequestMapping("/adoptMachine")
public class AdoptMachineController {
    /**
     * 金额格式化
     */
    private DecimalFormat df = new DecimalFormat("0.00");
    @Autowired
    private AdoptMachineBoundService adoptMachineBoundService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询领养车辆列表
     */
    @RequestMapping("/getByAccountUserId")
    public JsonResult send(String token) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        List<AdoptMachineBound> adoptMachineBounds = adoptMachineBoundService.getByAccountUserId(accountUserToken.getAccountUserId());

        return JsonResult.succ(adoptMachineBounds);
    }

    /**
     * 时间段统计
     */
    @RequestMapping("/staByDate")
    public JsonResult staByDate(String token, String startTime, String endTime) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        RideSta rideSta = adoptMachineBoundService.staByDate(accountUserToken.getAccountUserId(), startTime, endTime);

        /**时间格式化*/
        if (rideSta != null) {
            if (rideSta.getOrderMoney() == null) {
                rideSta.setOrderMoney(0D);
            }
            rideSta.setOrderMoney(Double.parseDouble(df.format(rideSta.getOrderMoney() / 100)));
        }

        return JsonResult.succ(rideSta);
    }

    /**
     * 按天统计
     */
    @RequestMapping("/orderStaGroupByDate")
    public JsonResult orderStaGroupByDate(String token, String startTime, String endTime) {
        AccountUser accountUserToken = tokenService.getAccount(token);

        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");

        List<OrderDateSta> orderDateStas = adoptMachineBoundService.orderStaGroupByDate(accountUserToken.getAccountUserId(), startTime, endTime);

        /**时间格式化*/
        for (OrderDateSta ods : orderDateStas) {
            ods.setOrderMoney(df.format(Double.parseDouble(ods.getOrderMoney()) / 100));
        }

        return JsonResult.succ(orderDateStas);
    }

}